<template>
    <el-dialog title="选择箱起礼包" :visible.sync="dialogVisible">
      <el-form label-position="left" label-width="120px" style="width: 100%; padding-left: 20px;">
        <el-form-item label="所选箱起礼包：" v-if="count !== 1">
          <div class="el-tag-box">
            <el-tag v-for="(item, index) of itemsChecked" :key="item.bonusGiftId" :closable="true" @close="delItem(index)">{{item.name}}</el-tag>
          </div>
        </el-form-item>
      </el-form>
      <el-form ref="searchForm" class="search-block" :model="listQuery" label-position="left" label-width="120px" style="width: 100%">
        <input type="text" style="display: none"/><!--form input placeholder-->
        <el-row :gutter="50">
          <el-col :span="12">
            <el-form-item label="箱起礼包名称：" prop="seriesName">
              <el-input @keyup.enter.native="search" v-model="listQuery.name" placeholder="按箱起礼包名称搜索" maxlength="50" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="箱起礼包类型：" prop="seriesName">
              <el-select @keyup.enter.native="search" v-model="listQuery.giftTypeStr" placeholder="请选择箱起礼包类型" clearable>
                <el-option value="2" label="云仓"></el-option>
                <el-option value="0,1" label="非云仓"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="" class="btn-item">
          <el-button type="primary" size="mini" icon="el-icon-search" plain @click="search">搜索</el-button>
        </el-form-item>
      </el-form>
        <!--<div class="form-item" v-if="count !== 1">-->
            <!--<span class="item-title">箱起礼包名称:</span>-->
            <!--<div class="el-tag-box">-->
                <!--<el-tag v-for="(item, index) of itemsChecked" :key="item.bonusGiftId" :closable="true" @close="delItem(index)">{{item.name}}</el-tag>-->
            <!--</div>-->
        <!--</div>-->
        <!--<div class="filter-container">-->
            <!--<el-input @keyup.enter.native="search" class="filter-item" placeholder="按名称搜索" v-model="listQuery.name"></el-input>-->
            <!--<el-button class="filter-item" type="primary" icon="el-icon-search" @click="search">搜索</el-button>-->
        <!--</div>-->
        <el-table :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border @row-click="clickRow">
            <el-table-column align="center" width="50">
                <template slot-scope="scope">
                    <label class="input-radio" v-if="count === 1">
                        <input type="radio" :value="{bonusGiftId: scope.row.bonusGiftId, name: scope.row.name, configStatus: scope.row.configStatus, startTime: scope.row.startTime, endTime: scope.row.endTime, imageMain: scope.row.imageMain}" v-model="itemRadio"/>
                        <span></span>
                    </label>
                    <label class="input-checkbox" v-else>
                        <input type="checkbox" :value="{bonusGiftId: scope.row.bonusGiftId, name: scope.row.name, imageMain: scope.row.imageMain}" v-model="itemsChecked"/>
                        <span></span>
                    </label>
                </template>
            </el-table-column>
            <el-table-column align="center" label="礼包名称" min-width="110">
                <template slot-scope="scope">
                    <div class="text-justify">
                        <p v-text="scope.row.name"></p>
                    </div>
                </template>
            </el-table-column>
            <el-table-column align="center" label="状态" prop="type" width="80">
              <template slot-scope="scope">
                <span v-if="scope.row.configStatus===0">活动中</span>
                <span v-else-if="scope.row.configStatus===1">未开始</span>
                <span v-else>已失效</span>
              </template>
            </el-table-column>
            <el-table-column align="center" label="生效时间" prop="startTime">
                <template slot-scope="scope">
                    <span>{{scope.row.startTime}}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="失效时间" prop="stopTime">
                <template slot-scope="scope">
                    <span>{{scope.row.endTime}}</span>
                </template>
            </el-table-column>
        </el-table>
        <div class="pagination-container" v-if="listTotal > 0">
            <el-pagination @current-change="handleCurrentChange" :current-page.sync="listQuery.pageIndex" :page-size="listQuery.pageSize" :total="listTotal" layout="total, prev, pager, next, jumper"></el-pagination>
        </div>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" :loading="btnDisabled" @click="ackChoose">确定</el-button>
            <el-button @click="cancelChoose">取消</el-button>
        </div>
    </el-dialog>
</template>
<script>
    export default {
        name: 'chooseActivity',
        props: {
            count: {
                type: Number,
                default: -1
            },
            check: {
                type: Array,
                default() {
                    return [];
                }
            }
        },
        data(){
            return {
                dialogVisible: true,
                itemRadio: {},
                itemsChecked: this.check,
                listQuery: {
                  pageIndex: 1,
                  pageSize: 10,
                  name: '',
                  giftTypeStr: ''
                },
                listLoading: false,
                list: [],
                listTotal: 0,
                btnDisabled: false
            }
        },
        created() {
          console.log('boxPackage get')
            this.getList();
            if (this.count === 1 && this.check.length > 0) {
                this.itemRadio = this.check[0];
            }
        },
        methods: {
            // 获取列表
            getList() {
                this.listLoading = true;
                this.request({
                    url: '/bsnl-product/box/bonus/gift/list',
                    method: 'post',
                    data: this.listQuery
                }).then(({data, total}) => {
                    this.list = data;
                    this.listTotal = total;
                    this.listLoading = false;
                }).catch(err => {
                    console.log(err);
                    this.list = [];
                    this.listTotal = 0;
                    this.listLoading = false;
                });
            },
            delItem(index){
                this.itemsChecked.splice(index, 1);
            },
            search(){
                this.listQuery.pageIndex = 1;
                this.getList();
            },
            clickRow({bonusGiftId, name, configStatus, startTime, endTime, imageMain}){
                if (this.count === 1) {
                    this.itemRadio = {
                        bonusGiftId,
                        name,
                        configStatus,
                        startTime,
                        endTime,
                        imageMain
                    };
                    return false;
                }
                let index = this.itemsChecked.findIndex(item => item.bonusGiftId === bonusGiftId);
                if (index < 0) {
                    this.itemsChecked.push({
                        bonusGiftId,
                        name,
                        imageMain
                    });
                } else {
                    this.itemsChecked.splice(index, 1);
                }
            },
            handleCurrentChange(val){
                this.listQuery.pageIndex = val;
                this.getList();
            },
            ackChoose(){
                if (this.count === 1 && this.itemRadio.bonusGiftId){
                    this.itemsChecked = [this.itemRadio]
                }
                if(this.itemsChecked.length < 1){
                    return this.$message.error('请选择活动');
                }
                if(this.count > 0 && this.itemsChecked.length > this.count){
                    return this.$message.error(`最多选择${this.count}个活动`);
                }
                this.$emit('choose', this.itemsChecked);
                this.dialogVisible = false;
                document.body.removeChild(this.$el);
            },
            cancelChoose(){
                this.dialogVisible = false;
                document.body.removeChild(this.$el);
            }
        }
    }
</script>
<style lang="scss" scoped>
  .pagination-container {
    /deep/ .el-pagination__editor.el-input {
      width: auto!important;
    }
  }
</style>
